#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mysql.h"
#include "dir_stack.h"

// 你之前写的 rmdir_order 函数声明，确保放在合适的头文件里或者这里声明
int rmdir_order(MYSQL *mysql, const char *user, const char *dirname, Link_list_t *catalog);

int main() {
    MYSQL *conn = NULL;
    const char *user = "test_user"; // 你测试用的用户名
    char result[4096] = {0};
    char stackPath[1024] = {0};

    // 连接数据库
    if (mysqlConnect(&conn) != 0) {
        printf(" 数据库连接失败\n");
        return -1;
    }
    printf(" 数据库连接成功\n");
    char ret[1024] = {0};
    log_on(conn,"kang","123456",ret);
    printf("%s\n",ret);
    // 初始化目录栈
    Link_list_t catalog;
    if (init_list(&catalog) != 0) {
        printf(" 目录栈初始化失败\n");
        return -1;
    }
    printf(" 初始化目录栈成功\n");

    char id_list[1024] = {0};
    // 创建目录
    if (mkdir_order(conn, "new_folder1", user, &catalog) == 0) {
        printf(" 创建目录 new_folder1 成功\n");
    } else {
        printf(" 创建目录 new_folder1 失败\n");
    }

    // 切换目录
    if (cd_order(conn, user, "new_folder1", &catalog) == 0) {
        printf(" 切换目录到 new_folder1 成功\n");
    } else {
        printf(" 切换目录1失败\n");
    }
    printf_Linklist(&catalog,id_list);//-------------------------------------------------------
    printf("%s\n",id_list);
    bzero(id_list,sizeof(id_list));
    // 创建目录
    if (mkdir_order(conn, "new_folder2", user, &catalog) == 0) {
        printf(" 创建目录 new_folder2 成功\n");
    } else {
        printf(" 创建目录 new_folder2 失败\n");
    }   
 // 切换目录
      if (cd_order(conn, user, "..", &catalog) == 0) {
          printf(" 切换目录到 new_folder1 成功\n");
      } else {
          printf(" 切换目录1失败\n");
      }  
      printf_Linklist(&catalog,id_list);//-------------------------------------------------------
          printf("%s\n",id_list);
      bzero(id_list,sizeof(id_list));
       // 切换目录
      if (cd_order(conn, user, "new_folder1/new_folder2/", &catalog) == 0) {
          printf(" 切换目录到 new_folder1/new_folder2 成功\n");
      } else {
          printf(" 切换目录失败\n");
      }
      printf_Linklist(&catalog,id_list);//-------------------------------------------------------
          printf("%s\n",id_list);
      bzero(id_list,sizeof(id_list));
    // ls 显示当前目录文件列表
    if (ls_order(conn, user, &catalog, result) == 0) {
        printf(" 当前目录文件列表：\n%s\n", result);
    } else {
        printf(" 读取文件列表失败\n");
    }

    // pwd 显示当前目录路径
    if (pwd(&catalog, conn, stackPath) == 0) {
        printf(" 当前路径为: %s\n", stackPath);
    } else {
        printf(" 获取路径失败\n");
    }
printf_Linklist(&catalog,id_list);//-------------------------------------------------------     
          printf("%s\n",id_list);
      bzero(id_list,sizeof(id_list));
    // rmdir 测试部分
    // 先切回上级目录，方便删除 new_folder
    if (cd_order(conn, user, "..", &catalog) == 0) {
        printf(" 切换目录到上级目录成功\n");
    } else {
        printf(" 切换目录到上级目录失败\n");
    }

    // pwd 显示当前目录路径
    if (pwd(&catalog, conn, stackPath) == 0) {
        printf(" 当前路径为: %s\n", stackPath);
    } else {
        printf(" 获取路径失败\n");
    }   
printf_Linklist(&catalog,id_list);//-------------------------------------------------------     
          printf("%s\n",id_list);
      bzero(id_list,sizeof(id_list));
 // 切换目录
        if (cd_order(conn, user, "../new_folder1/../new_folder1/new_folder2", &catalog) == 0) {
            printf(" 切换目录到 ../new_folder1/../new_folder1/new_folder2 成功\n");
        } else {
            printf(" 切换目录 ../new_folder1/../new_folder1/new_folder2 失败\n");
        }
         // pwd 显示当前目录路径
    if (pwd(&catalog, conn, stackPath) == 0) {
        printf(" 当前路径为: %s\n", stackPath);
    } else {
        printf(" 获取路径失败\n");
    }
   // 删除 new_folder 目录（前提是该目录为空）                                                                                                                                            
     if (rmdir_order(conn, user, "new_folder2", &catalog) == 0) {
         printf(" 删除目录 new_folder2 成功\n");
     } else {
         printf(" 删除目录 new_folder2 失败\n");
     }
    printf_Linklist(&catalog,id_list);//-------------------------------------------------------     
           printf("%s\n",id_list);
       bzero(id_list,sizeof(id_list));


    // 关闭数据库连接
    mysql_close(conn);

    return 0;
}

